package com.lj.hadoop;

import java.io.IOException;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IOUtils;
import org.apache.hadoop.io.SequenceFile;
import org.apache.hadoop.io.SequenceFile.Reader;
import org.apache.hadoop.io.Writable;
import org.apache.hadoop.util.ReflectionUtils;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class SequenFileReadDemo {

	public static void main(String[] args) throws IOException {
		// TODO Auto-generated method stub
		String uri = args[0];
		ApplicationContext ctx = new ClassPathXmlApplicationContext(
				"classpath:spring/applicationContext.xml");
		Configuration conf = ctx.getBean("hadoopConfiguration",
				Configuration.class);
		Path path = new Path(uri);
		SequenceFile.Reader read = null;
		try{
			read = new SequenceFile.Reader(conf, Reader.file(path));
			Writable key = (Writable)ReflectionUtils.newInstance(read.getKeyClass(), conf);
			Writable value = (Writable)ReflectionUtils.newInstance(read.getValueClass(), conf);
			long position = read.getPosition();
			while(read.next(key, value)){
				//一个标记点
				String syncSeen = read.syncSeen()?"*":"";
				System.out.printf("[%s%s]\t%s\t%s\n", position, syncSeen,key,
						value);
				position = read.getPosition();
				
			}
		}finally{
			IOUtils.closeStream(read);
		}
		

	}

}
